import Vue from "vue";
import VueRouter from "vue-router";
import Layout from "../layout/Layout.vue";
import Apply from "../views/apply/Apply.vue";
import Charge from "../views/charge/Charge.vue";
import Findpwd from "../views/findpwd/Findpwd.vue";
import Login from "../views/login/Login.vue";
import Management from "../views/management/Management.vue";
import Medicine from "../views/medicine/Medicine.vue";
import Newcome from "../views/newcome/Newcome.vue";
import Register from "../views/register/Register.vue";
import VIP from "../views/VIP/VIP.vue";
import Workspace from "../views/workspace/Workspace.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/layout",
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/findpwd",
    name: "Findpwd",
    component: Findpwd,
  },
  {
    path: "/apply",
    name: "Apply",
    component: Apply,
  },
  {
    path: "/layout",
    name: "Layout",
    component: Layout,
    redirect: "/layout/management",
    children: [
      {
        path: "charge",
        name: "Charge",
        component: Charge,
        meta: { title: "收费管理" },
      },

      {
        path: "management",
        name: "Management",
        component: Management,
        meta: { title: "经营管理" },
      },
      {
        path: "medicine",
        name: "Medicine",
        component: Medicine,
        meta: { title: "药品零售" },
      },
      {
        path: "newcome",
        name: "Newcome",
        component: Newcome,
        meta: { title: "新开就诊" },
      },
      {
        path: "register",
        name: "Register",
        component: Register,
        meta: { title: "挂号管理" },
      },
      {
        path: "VIP",
        name: "VIP",
        component: VIP,
        meta: { title: "会员管理" },
      },
      {
        path: "workspace",
        name: "Workspace",
        component: Workspace,
        meta: { title: "工作台" },
      },
    ],
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

// 白名单
const whileArr = ["/login", "/apply", "/findpwd"];
// 权限路由
import { getToken } from "../utils/cookieUtils";
import { getUserBase } from "../api/routeApi";
import store from "../store/index";
let nickobj = [];
export default router;
router.beforeEach((to, from, next) => {
  let mypath = to.path;
  let token = getToken("token");
  if (token) {
    if (mypath == "/login") {
      next("/");
    } else {
      // 制作动态菜单
      getUserBase({ token: token }).then((res) => {
        // 讲路由放入vuex中保存
        // console.log(routes[2].children);
        nickobj.push(res.data.nick);
        // console.log(nickobj);
        store.dispatch("addRoutesAction", { routes, nickobj }).then(() => {
          // console.log(res.data.nick);
          // 菜单生效 刷新所有路由
          router.addRoutes(routes, nickobj);
          next({ ...to, replace: true });
        });
      });
    }
  } else {
    if (whileArr.includes(mypath)) {
      next();
    } else {
      next("/login");
    }
  }
  next();
});
